<!doctype html>
<html>
<head>
    <!--Setting-->
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <meta name="renderer" content="webkit|ie-comp|ie-stand">
    <meta name="apple-mobile-web-app-capable"  content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="format-detection" content="telephone=no,email=no">
    
    
    <!--Simple SEO-->


<meta name="robots" content=all />
<meta name="google" content=all />
<meta name="googlebot" content=all />
<meta name="verify" content=all />
    <!--Title-->

<title>mongodb集群安装 | MIAbon&#39;s blog</title>

<link rel="alternate" href="/atom.xml" title="MIAbon&#39;s blog" type="application/atom+xml">


<link rel="icon" href="/favicon.ico">

    
<link rel="stylesheet" href="/css/base.css">
<link rel="stylesheet" href="/css/pages/post.css">
<link rel="stylesheet" href="//cdn.bootcss.com/font-awesome/4.7.0/css/font-awesome.min.css">
<link rel="stylesheet" href="/css/thirdParty/highlight/github.css">
<link rel="stylesheet" href="/.css">

    <!--script-->


<script src="http://cdn1.lncld.net/static/js/3.2.1/av-min.js"></script>
<script>
  var appId = "i7AHmC7NPbPtgS3YxT67dRIc-9Nh9j0Va";
  var appKey = "g6TKbY8O4TsCDMcemoC3STvQ";
  var region = "";
  AV.init({
    appId: appId,
    appKey: appKey,
    region: region
  });
</script>


<script async src="//dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"></script>

<!--<script src="https://imsun.github.io/gitment/dist/gitment.browser.js"></script>-->


    
    
</head>

<body id="normal">
<!--[if lte IE 8]>
<style>
    html{ font-size: 1em }
</style>
<![endif]-->
<!--[if lte IE 9]>
<style>
    header{ top: 71px; position: absolute!important;}
    #container{padding-top: 151px!important;}
</style>
<div style="position:fixed;z-index:9999;left:0;top:0;width:100%;height:70px;background-color:#e0e0e0;color:#396CA5;border-bottom:1px solid #cecece;text-align:center;line-height:70px;white-space: nowrap;overflow: hidden;text-overflow: ellipsis">你使用的浏览器版本过低，为了你更好的阅读体验，请更新浏览器的版本或者使用其他现代浏览器，比如Chrome、Firefox、Safari等。</div>
<![endif]-->

<div id="wrap">
    <header  style="position: absolute;" >
    <div id="site-meta">
        <a href="/" id="logo">
            <h1 class="title">MIAbon&#39;s blog</h1>
        </a>
        
    </div>
    <ul id="nav">
        
            <li><a href="/"><i class="fa fa-home"></i>首页</a></li>
        
            <li><a href="/atom.xml"><i class="fa fa-rss"></i>RSS</a></li>
        
        <li id="search"><a href="javascript:void(0)"><i class="fa fa-search"></i>搜索</a></li>
    </ul>
</header>

    <div id="container">
        
<ul id="sidebar">
    
    
    
    
<li class="widget widget-normal category">
    <h3 class="fa fa-th widget-title">分类</h3>
    <ul class="category-list"><li class="category-list-item"><a class="category-list-link" href="/categories/ELK/"><i class="fa" aria-hidden="true">ELK</i></a><span class="category-list-count">1</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/Lvs/"><i class="fa" aria-hidden="true">Lvs</i></a><span class="category-list-count">2</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/admin/"><i class="fa" aria-hidden="true">admin</i></a><span class="category-list-count">5</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/ansible/"><i class="fa" aria-hidden="true">ansible</i></a><span class="category-list-count">1</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/aws/"><i class="fa" aria-hidden="true">aws</i></a><span class="category-list-count">1</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/docker/"><i class="fa" aria-hidden="true">docker</i></a><span class="category-list-count">7</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/drone/"><i class="fa" aria-hidden="true">drone</i></a><span class="category-list-count">1</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/kernel/"><i class="fa" aria-hidden="true">kernel</i></a><span class="category-list-count">2</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/kubernetes/"><i class="fa" aria-hidden="true">kubernetes</i></a><span class="category-list-count">2</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/kvm/"><i class="fa" aria-hidden="true">kvm</i></a><span class="category-list-count">1</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/mongodb/"><i class="fa" aria-hidden="true">mongodb</i></a><span class="category-list-count">1</span></li><li class="category-list-item"><a class="category-list-link current" href="/categories/python/"><i class="fa" aria-hidden="true">python</i></a><span class="category-list-count">9</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/share/"><i class="fa" aria-hidden="true">share</i></a><span class="category-list-count">1</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/web/"><i class="fa" aria-hidden="true">web</i></a><span class="category-list-count">12</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/zabbix/"><i class="fa" aria-hidden="true">zabbix</i></a><span class="category-list-count">3</span></li></ul>
</li>


    
    
<li class="widget widget-normal archive">
  <h3 class="fa fa-archive widget-title">归档</h3>
    <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/02/"><i class="fa" aria-hidden="true">二月 2018</i></a><span class="archive-list-count">1</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/01/"><i class="fa" aria-hidden="true">一月 2018</i></a><span class="archive-list-count">8</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2017/12/"><i class="fa" aria-hidden="true">十二月 2017</i></a><span class="archive-list-count">1</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2017/11/"><i class="fa" aria-hidden="true">十一月 2017</i></a><span class="archive-list-count">3</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2017/10/"><i class="fa" aria-hidden="true">十月 2017</i></a><span class="archive-list-count">4</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2017/09/"><i class="fa" aria-hidden="true">九月 2017</i></a><span class="archive-list-count">2</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2017/08/"><i class="fa" aria-hidden="true">八月 2017</i></a><span class="archive-list-count">1</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2017/05/"><i class="fa" aria-hidden="true">五月 2017</i></a><span class="archive-list-count">4</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2017/04/"><i class="fa" aria-hidden="true">四月 2017</i></a><span class="archive-list-count">2</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2017/03/"><i class="fa" aria-hidden="true">三月 2017</i></a><span class="archive-list-count">1</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2017/01/"><i class="fa" aria-hidden="true">一月 2017</i></a><span class="archive-list-count">1</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2016/12/"><i class="fa" aria-hidden="true">十二月 2016</i></a><span class="archive-list-count">1</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2016/11/"><i class="fa" aria-hidden="true">十一月 2016</i></a><span class="archive-list-count">3</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2016/10/"><i class="fa" aria-hidden="true">十月 2016</i></a><span class="archive-list-count">2</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2016/08/"><i class="fa" aria-hidden="true">八月 2016</i></a><span class="archive-list-count">2</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2016/05/"><i class="fa" aria-hidden="true">五月 2016</i></a><span class="archive-list-count">4</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2016/04/"><i class="fa" aria-hidden="true">四月 2016</i></a><span class="archive-list-count">8</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2015/05/"><i class="fa" aria-hidden="true">五月 2015</i></a><span class="archive-list-count">1</span></li></ul>
</li>


    
    
<li class="widget widget-normal tags">
  <h3 class="fa fa-tags widget-title">标签云</h3>
  <div class="tagcloud-content">
    
      <a href="/tags/drone/" style="font-size: 0.14rem; color: #69c">drone</a> <a href="/tags/linux/" style="font-size: 0.2rem; color: #0a407c">linux</a> <a href="/tags/kernel/" style="font-size: 0.16rem; color: #4f83b8">kernel</a> <a href="/tags/ELK/" style="font-size: 0.14rem; color: #69c">ELK</a> <a href="/tags/ansible/" style="font-size: 0.14rem; color: #69c">ansible</a> <a href="/tags/swarm/" style="font-size: 0.14rem; color: #69c">swarm</a> <a href="/tags/docker/" style="font-size: 0.18rem; color: #215690">docker</a> <a href="/tags/firewalld/" style="font-size: 0.14rem; color: #69c">firewalld</a> <a href="/tags/zabbix/" style="font-size: 0.16rem; color: #4f83b8">zabbix</a> <a href="/tags/aws/" style="font-size: 0.14rem; color: #69c">aws</a> <a href="/tags/web/" style="font-size: 0.16rem; color: #4f83b8">web</a> <a href="/tags/python/" style="font-size: 0.18rem; color: #215690">python</a> <a href="/tags/spider/" style="font-size: 0.17rem; color: #386da4">spider</a>
  </div>
</li>


    
    
<li class="widget widget-normal friends-link">
    <h3 class="fa fa-globe widget-title">友链</h3><br/>

    
        <a href="http://zhengwei666.wang" class="fa" target="_blank">zhengwei</a>

    
        <a href="https://you-deng.github.io" class="fa" target="_blank">dengyou</a>

    
        <a href="http://www.systemd.cn" class="fa" target="_blank">langyaoliang</a>

    
        <a href="http://www.chen-hao.com.cn/" class="fa" target="_blank">chenhao</a>

    
        <a href="http://www.yulongjun.com" class="fa" target="_blank">yulongjun</a>

    

</li>

    
</ul>


        <div id="main">
    <article id="post">
        <div id="post-header">

            <h1 id="mongodb集群安装">
                
                mongodb集群安装
                
            </h1>
            <div class="article-meta">
    
    
    <span class="categories-meta fa-wrap">
            <i class="fa fa-folder-open-o"></i>
        <span>mongodb</span>
    </span>
    
    
    <span class="fa-wrap">
         <i class="fa fa-tags"></i>
        <span class="tags-meta">
            
            linux
            
        </span>
    </span>
    
    
    <span class="fa-wrap">
        <i class="fa fa-clock-o"></i>
        <span class="date-meta ">2016/10/31</span>
    </span>
    
    
    <span class="fa-wrap">
            <i class="fa fa-thermometer-three-quarters"></i>
        <span class="hits hits-meta " data-leadcloud-title="mongodb集群安装"
              data-leadcloud-url="/2016/10/31/mongodb集群安装/"><i class="fa fa-spinner fa-spin"></i></span>
    </span>
    
    
</div>

            
            
            <p class="fa fa-exclamation-triangle warning">
                本文于<strong>459</strong>天之前发表，文中内容可能已经过时。如有疑问，请在评论区留言。
            </p>
            
        </div>
        
        <div id="post-body">
            <h1 id="mongodb安装"><a href="#mongodb安装" class="headerlink" title="mongodb安装"></a>mongodb安装</h1><p><strong>下载mongodb源码包</strong><br>下载地址：<em><a href="http://www.mongodb.org/downloads" target="_blank" rel="external">http://www.mongodb.org/downloads</a></em><br>解压缩并将解压后目录mv至安装位置<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">tar -xvf mongodb-linux-x86_64-3.0.6.tgz</div><div class="line">mv mongodb-linux-x86_64-3.0.6 /usr/<span class="built_in">local</span>/mongodb</div></pre></td></tr></table></figure></p>
<p>修改PATH环境变量指明mongodb的可执行文件<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line"> <span class="built_in">cd</span>  /etc/profile.d/</div><div class="line">cat mongodb.sh </div><div class="line"><span class="built_in">export</span> PATH=<span class="variable">$PATH</span>:/usr/<span class="built_in">local</span>/mongodb/bin</div><div class="line"><span class="built_in">source</span> mongodb.sh</div><div class="line"><span class="built_in">echo</span> <span class="variable">$PATH</span></div><div class="line">/usr/<span class="built_in">local</span>/sbin:/usr/<span class="built_in">local</span>/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin:/usr/<span class="built_in">local</span>/mongodb/bin</div></pre></td></tr></table></figure></p>
<p>本文档在两台服务器上创建了一个集群</p>
<h2 id="NTP时间同步（所有服务器时间必须同步否则集群无法建立）"><a href="#NTP时间同步（所有服务器时间必须同步否则集群无法建立）" class="headerlink" title="NTP时间同步（所有服务器时间必须同步否则集群无法建立）"></a>NTP时间同步（所有服务器时间必须同步否则集群无法建立）</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">ntpdate cn.pool.ntp.org</div></pre></td></tr></table></figure>
<h2 id="创建数据目录"><a href="#创建数据目录" class="headerlink" title="创建数据目录"></a>创建数据目录</h2><h3 id="在服务器A上"><a href="#在服务器A上" class="headerlink" title="在服务器A上"></a>在服务器A上</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">mkdir -pv /data/mongodb/shard1_1</div><div class="line">mkdir -pv /data/mongodb/shard2_1</div><div class="line">mkdir -pv /data/mongodb/config</div><div class="line">mkdir -pv /data/mongodb/logs</div></pre></td></tr></table></figure>
<h3 id="在服务器B上"><a href="#在服务器B上" class="headerlink" title="在服务器B上"></a>在服务器B上</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">mkdir -pv /data/mongodb/shard1_2</div><div class="line">mkdir -pv /data/mongodb/shard2_2</div><div class="line">mkdir -pv /data/mongodb/config</div><div class="line">mkdir -pv /data/mongodb/logs</div></pre></td></tr></table></figure>
<h3 id="在服务器C上"><a href="#在服务器C上" class="headerlink" title="在服务器C上"></a>在服务器C上</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">mkdir -pv /data/mongodb/shard1_3</div><div class="line">mkdir -pv /data/mongodb/shard2_3</div><div class="line">mkdir -pv /data/mongodb/config</div><div class="line">mkdir -pv /data/mongodb/logs</div></pre></td></tr></table></figure>
<p>配置Replica Sets<br>配置shard1的replica sets<br>在服务器A启动shard1的1（主）<br>提供配置文件<br>分片1的配置文件<br><strong>shard1_1_27017.conf</strong><br><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div></pre></td><td class="code"><pre><div class="line">dbpath=/data/mongodb/shard1_1</div><div class="line">logpath=/data/mongodb/logs/shard1_1_27017.log</div><div class="line">pidfilepath=/data/mongodb/shard1_1/shard1_1_27017.pid</div><div class="line">directoryperdb=<span class="literal">true</span></div><div class="line">logappend=<span class="literal">true</span></div><div class="line">replSet=shard1</div><div class="line">bind_ip=192.168.100.142</div><div class="line">port=27017</div><div class="line">shardsvr=<span class="literal">true</span></div><div class="line">fork=<span class="literal">true</span></div><div class="line">nojournal = <span class="literal">true</span></div><div class="line">smallfiles = <span class="literal">true</span></div><div class="line">noprealloc = <span class="literal">true</span></div></pre></td></tr></table></figure></p>
<p><strong>shard1_2_27017.conf</strong><br><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div></pre></td><td class="code"><pre><div class="line">dbpath=/data/mongodb/shard1_2</div><div class="line">logpath=/data/mongodb/logs/shard1_2_27017.log</div><div class="line">pidfilepath=/data/mongodb/shard1_2/shard1_2_27017.pid</div><div class="line">directoryperdb=<span class="literal">true</span></div><div class="line">logappend=<span class="literal">true</span></div><div class="line">replSet=shard1</div><div class="line">bind_ip=192.168.100.178</div><div class="line">port=27017</div><div class="line">shardsvr=<span class="literal">true</span></div><div class="line">fork=<span class="literal">true</span></div><div class="line">nojournal=<span class="literal">true</span></div><div class="line">smallfiles=<span class="literal">true</span></div><div class="line">noprealloc=<span class="literal">true</span></div></pre></td></tr></table></figure></p>
<p><strong>shard1_3_27017.conf</strong><br><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div></pre></td><td class="code"><pre><div class="line">dbpath=/data/mongodb/shard1_3</div><div class="line">logpath=/data/mongodb/logs/shard1_3_27017.log</div><div class="line">pidfilepath=/data/mongodb/shard1_3/shard1_3_27017.pid</div><div class="line">directoryperdb=<span class="literal">true</span></div><div class="line">logappend=<span class="literal">true</span></div><div class="line">replSet=shard1</div><div class="line">bind_ip=192.168.100.180</div><div class="line">port=27017</div><div class="line">shardsvr=<span class="literal">true</span></div><div class="line">fork=<span class="literal">true</span></div><div class="line">nojournal=<span class="literal">true</span></div><div class="line">smallfiles=<span class="literal">true</span></div><div class="line">noprealloc=<span class="literal">true</span></div></pre></td></tr></table></figure></p>
<p>分片2的配置文件<br><strong>shard2_1_27018.conf</strong><br><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div></pre></td><td class="code"><pre><div class="line">dbpath=/data/mongodb/shard2_1</div><div class="line">logpath=/data/mongodb/logs/shard2_1_27018.log</div><div class="line">pidfilepath=/data/mongodb/shard2_1/shard2_1_27018.pid</div><div class="line">directoryperdb=<span class="literal">true</span></div><div class="line">logappend=<span class="literal">true</span></div><div class="line">replSet=shard2</div><div class="line">bind_ip=192.168.100.142</div><div class="line">port=27018</div><div class="line">shardsvr=<span class="literal">true</span></div><div class="line">fork=<span class="literal">true</span></div><div class="line">nojournal=<span class="literal">true</span></div><div class="line">smallfiles=<span class="literal">true</span></div><div class="line">noprealloc=<span class="literal">true</span></div></pre></td></tr></table></figure></p>
<p><strong>shard2_2_27018.conf</strong><br><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div></pre></td><td class="code"><pre><div class="line">dbpath=/data/mongodb/shard2_2</div><div class="line">logpath=/data/mongodb/logs/shard2_2_27018.log</div><div class="line">pidfilepath=/data/mongodb/shard2_2/shard2_2_27018.pid</div><div class="line">directoryperdb=<span class="literal">true</span></div><div class="line">logappend=<span class="literal">true</span></div><div class="line">replSet=shard2</div><div class="line">bind_ip=192.168.100.178</div><div class="line">port=27018</div><div class="line">shardsvr=<span class="literal">true</span></div><div class="line">fork=<span class="literal">true</span></div><div class="line">nojournal=<span class="literal">true</span></div><div class="line">smallfiles=<span class="literal">true</span></div><div class="line">noprealloc=<span class="literal">true</span></div></pre></td></tr></table></figure></p>
<p><strong>shard2_3_27018.conf</strong><br><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div></pre></td><td class="code"><pre><div class="line">dbpath=/data/mongodb/shard2_3</div><div class="line">logpath=/data/mongodb/logs/shard2_3_27018.log</div><div class="line">pidfilepath=/data/mongodb/shard2_3/shard2_3_27018.pid</div><div class="line">directoryperdb=<span class="literal">true</span></div><div class="line">logappend=<span class="literal">true</span></div><div class="line">replSet=shard2</div><div class="line">bind_ip=192.168.100.180</div><div class="line">port=27018</div><div class="line">shardsvr=<span class="literal">true</span></div><div class="line">fork=<span class="literal">true</span></div><div class="line">nojournal=<span class="literal">true</span></div><div class="line">smallfiles=<span class="literal">true</span></div><div class="line">noprealloc=<span class="literal">true</span></div></pre></td></tr></table></figure></p>
<h2 id="启动分片1"><a href="#启动分片1" class="headerlink" title="启动分片1"></a>启动分片1</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">[root@localhost shard1_1]<span class="comment"># mongod   -f shard1_1_27017.conf</span></div><div class="line">note: noprealloc may hurt performance <span class="keyword">in</span> many applications</div><div class="line">about to fork child process, waiting until server is ready <span class="keyword">for</span> connections.</div><div class="line">forked process: 1243</div><div class="line">child process started successfully, parent exiting</div><div class="line">[root@localhost shard1_1]<span class="comment">#</span></div></pre></td></tr></table></figure>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">[root@ansible shard1_2]<span class="comment"># mongod -f  shard1_2_27017.conf</span></div><div class="line">note: noprealloc may hurt performance <span class="keyword">in</span> many applications</div><div class="line">about to fork child process, waiting until server is ready <span class="keyword">for</span> connections.</div><div class="line">forked process: 27445</div><div class="line">child process started successfully, parent exiting</div><div class="line">[root@ansible shard1_2]<span class="comment">#</span></div></pre></td></tr></table></figure>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">[root@localhost shard1_3]<span class="comment"># mongod -f shard1_3_27017.conf</span></div><div class="line">note: noprealloc may hurt performance <span class="keyword">in</span> many applications</div><div class="line">about to fork child process, waiting until server is ready <span class="keyword">for</span> connections.</div><div class="line">forked process: 1714</div><div class="line">child process started successfully, parent exiting</div><div class="line">[root@localhost shard1_3]</div></pre></td></tr></table></figure>
<h2 id="启动分片2"><a href="#启动分片2" class="headerlink" title="启动分片2"></a>启动分片2</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">[root@localhost shard2_1]<span class="comment"># mongod -f shard2_1_27018.conf</span></div><div class="line">note: noprealloc may hurt performance <span class="keyword">in</span> many applications</div><div class="line">about to fork child process, waiting until server is ready <span class="keyword">for</span> connections.</div><div class="line">forked process: 1278</div><div class="line">child process started successfully, parent exiting</div><div class="line">[root@localhost shard2_1]<span class="comment">#</span></div></pre></td></tr></table></figure>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">[root@ansible shard2_2]<span class="comment"># mongod -f shard2_2_27018.conf</span></div><div class="line">note: noprealloc may hurt performance <span class="keyword">in</span> many applications</div><div class="line">about to fork child process, waiting until server is ready <span class="keyword">for</span> connections.</div><div class="line">forked process: 27496</div><div class="line">child process started successfully, parent exiting</div><div class="line">[root@ansible shard2_2]<span class="comment">#</span></div></pre></td></tr></table></figure>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">[root@localhost shard2_3]<span class="comment"># mongod -f  shard2_3_27018.conf</span></div><div class="line">note: noprealloc may hurt performance <span class="keyword">in</span> many applications</div><div class="line">about to fork child process, waiting until server is ready <span class="keyword">for</span> connections.</div><div class="line">forked process: 1747</div><div class="line">child process started successfully, parent exiting</div><div class="line">[root@localhost shard2_3]<span class="comment">#</span></div></pre></td></tr></table></figure>
<p><strong>用mongo连接分片1的主节点，这里指服务器A的27017端口的mongod,初始化replica sets的“shard1”</strong><br><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div><div class="line">56</div><div class="line">57</div><div class="line">58</div><div class="line">59</div><div class="line">60</div><div class="line">61</div><div class="line">62</div><div class="line">63</div><div class="line">64</div><div class="line">65</div><div class="line">66</div><div class="line">67</div><div class="line">68</div><div class="line">69</div><div class="line">70</div><div class="line">71</div><div class="line">72</div><div class="line">73</div><div class="line">74</div><div class="line">75</div><div class="line">76</div><div class="line">77</div><div class="line">78</div></pre></td><td class="code"><pre><div class="line">[root@localhost shard2_1]<span class="comment"># /usr/local/mongodb/bin/mongo  192.168.100.142:27017</span></div><div class="line">MongoDB shell version: 3.0.6</div><div class="line">connecting to: 192.168.100.142:27017/<span class="built_in">test</span></div><div class="line">Welcome to the MongoDB shell.</div><div class="line">&gt; config=&#123; _id:<span class="string">"shard1"</span>, members:[ &#123;_id:0,host:<span class="string">'192.168.100.142:27017'</span>,priority:2&#125;, &#123;_id:1,host:<span class="string">'192.168.100.178:27017'</span>,priority:1&#125;,&#123;_id:2,host:<span class="string">'192.168.100.180:27017'</span>,arbiterOnly:<span class="literal">true</span>&#125;] &#125;;</div><div class="line">&#123;</div><div class="line">    <span class="string">"_id"</span> : <span class="string">"shard1"</span>,</div><div class="line">    <span class="string">"members"</span> : [</div><div class="line">        &#123;</div><div class="line">            <span class="string">"_id"</span> : 0,</div><div class="line">            <span class="string">"host"</span> : <span class="string">"192.168.100.142:27017"</span>,</div><div class="line">            <span class="string">"priority"</span> : 2</div><div class="line">        &#125;,</div><div class="line">        &#123;</div><div class="line">            <span class="string">"_id"</span> : 1,</div><div class="line">            <span class="string">"host"</span> : <span class="string">"192.168.100.178:27017"</span>,</div><div class="line">            <span class="string">"priority"</span> : 1</div><div class="line">        &#125;,</div><div class="line">        &#123;</div><div class="line">            <span class="string">"_id"</span> : 2,</div><div class="line">            <span class="string">"host"</span> : <span class="string">"192.168.100.180:27017"</span>,</div><div class="line">            <span class="string">"arbiterOnly"</span> : <span class="literal">true</span></div><div class="line">        &#125;</div><div class="line">    ]</div><div class="line">&#125;</div><div class="line">&gt; rs.initiate(config)</div><div class="line">&#123; <span class="string">"ok"</span> : 1 &#125;</div><div class="line">shard1:OTHER&gt; rs.status()</div><div class="line">&#123;</div><div class="line">    <span class="string">"set"</span> : <span class="string">"shard1"</span>,</div><div class="line">    <span class="string">"date"</span> : ISODate(<span class="string">"2016-09-08T03:57:34.172Z"</span>),</div><div class="line">    <span class="string">"myState"</span> : 1,</div><div class="line">    <span class="string">"members"</span> : [</div><div class="line">        &#123;</div><div class="line">            <span class="string">"_id"</span> : 0,</div><div class="line">            <span class="string">"name"</span> : <span class="string">"192.168.100.142:27017"</span>,</div><div class="line">            <span class="string">"health"</span> : 1,</div><div class="line">            <span class="string">"state"</span> : 1,</div><div class="line">            <span class="string">"stateStr"</span> : <span class="string">"PRIMARY"</span>,</div><div class="line">            <span class="string">"uptime"</span> : 1562,</div><div class="line">            <span class="string">"optime"</span> : Timestamp(1473307037, 1),</div><div class="line">            <span class="string">"optimeDate"</span> : ISODate(<span class="string">"2016-09-08T03:57:17Z"</span>),</div><div class="line">            <span class="string">"electionTime"</span> : Timestamp(1473307041, 1),</div><div class="line">            <span class="string">"electionDate"</span> : ISODate(<span class="string">"2016-09-08T03:57:21Z"</span>),</div><div class="line">            <span class="string">"configVersion"</span> : 1,</div><div class="line">            <span class="string">"self"</span> : <span class="literal">true</span></div><div class="line">        &#125;,</div><div class="line">        &#123;</div><div class="line">            <span class="string">"_id"</span> : 1,</div><div class="line">            <span class="string">"name"</span> : <span class="string">"192.168.100.178:27017"</span>,</div><div class="line">            <span class="string">"health"</span> : 1,</div><div class="line">            <span class="string">"state"</span> : 2,</div><div class="line">            <span class="string">"stateStr"</span> : <span class="string">"SECONDARY"</span>,</div><div class="line">            <span class="string">"uptime"</span> : 17,</div><div class="line">            <span class="string">"optime"</span> : Timestamp(1473307037, 1),</div><div class="line">            <span class="string">"optimeDate"</span> : ISODate(<span class="string">"2016-09-08T03:57:17Z"</span>),</div><div class="line">            <span class="string">"lastHeartbeat"</span> : ISODate(<span class="string">"2016-09-08T03:57:33.096Z"</span>),</div><div class="line">            <span class="string">"lastHeartbeatRecv"</span> : ISODate(<span class="string">"2016-09-08T03:57:33.095Z"</span>),</div><div class="line">            <span class="string">"pingMs"</span> : 0,</div><div class="line">            <span class="string">"lastHeartbeatMessage"</span> : <span class="string">"could not find member to sync from"</span>,</div><div class="line">            <span class="string">"configVersion"</span> : 1</div><div class="line">        &#125;,</div><div class="line">        &#123;</div><div class="line">            <span class="string">"_id"</span> : 2,</div><div class="line">            <span class="string">"name"</span> : <span class="string">"192.168.100.180:27017"</span>,</div><div class="line">            <span class="string">"health"</span> : 1,</div><div class="line">            <span class="string">"state"</span> : 7,</div><div class="line">            <span class="string">"stateStr"</span> : <span class="string">"ARBITER"</span>,</div><div class="line">            <span class="string">"uptime"</span> : 17,</div><div class="line">            <span class="string">"lastHeartbeat"</span> : ISODate(<span class="string">"2016-09-08T03:57:33.095Z"</span>),</div><div class="line">            <span class="string">"lastHeartbeatRecv"</span> : ISODate(<span class="string">"2016-09-08T03:57:33.095Z"</span>),</div><div class="line">            <span class="string">"pingMs"</span> : 1,</div><div class="line">            <span class="string">"configVersion"</span> : 1</div><div class="line">        &#125;</div><div class="line">    ],</div><div class="line">    <span class="string">"ok"</span> : 1</div><div class="line">&#125;</div><div class="line">shard1:PRIMARY&gt;</div></pre></td></tr></table></figure></p>
<p><strong>用mongo连接分片2的主节点，这里是服务器B的27018端口的mongod,初始化replica sets的“shard2”</strong><br><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div><div class="line">56</div><div class="line">57</div><div class="line">58</div><div class="line">59</div><div class="line">60</div><div class="line">61</div><div class="line">62</div><div class="line">63</div><div class="line">64</div><div class="line">65</div><div class="line">66</div><div class="line">67</div><div class="line">68</div><div class="line">69</div><div class="line">70</div><div class="line">71</div><div class="line">72</div><div class="line">73</div><div class="line">74</div><div class="line">75</div><div class="line">76</div><div class="line">77</div><div class="line">78</div><div class="line">79</div></pre></td><td class="code"><pre><div class="line">[root@ansible shard2_2]<span class="comment">#  /usr/local/mongodb/bin/mongo  192.168.100.178:27018</span></div><div class="line">MongoDB shell version: 3.0.6</div><div class="line">connecting to: 192.168.100.178:27018/<span class="built_in">test</span></div><div class="line">&gt; config=&#123; _id:<span class="string">"shard2"</span>, members:[ &#123;_id:0,host:<span class="string">'192.168.100.178:27018'</span>,priority:2&#125;, &#123;_id:1,host:<span class="string">'192.168.100.180:27018'</span>,priority:1&#125;,&#123;_id:2,host:<span class="string">'192.168.100.142:27018'</span>,arbiterOnly:<span class="literal">true</span>&#125;] &#125;;</div><div class="line">&#123;</div><div class="line">    <span class="string">"_id"</span> : <span class="string">"shard2"</span>,</div><div class="line">    <span class="string">"members"</span> : [</div><div class="line">        &#123;</div><div class="line">            <span class="string">"_id"</span> : 0,</div><div class="line">            <span class="string">"host"</span> : <span class="string">"192.168.100.178:27018"</span>,</div><div class="line">            <span class="string">"priority"</span> : 2</div><div class="line">        &#125;,</div><div class="line">        &#123;</div><div class="line">            <span class="string">"_id"</span> : 1,</div><div class="line">            <span class="string">"host"</span> : <span class="string">"192.168.100.180:27018"</span>,</div><div class="line">            <span class="string">"priority"</span> : 1</div><div class="line">        &#125;,</div><div class="line">        &#123;</div><div class="line">            <span class="string">"_id"</span> : 2,</div><div class="line">            <span class="string">"host"</span> : <span class="string">"192.168.100.142:27018"</span>,</div><div class="line">            <span class="string">"arbiterOnly"</span> : <span class="literal">true</span></div><div class="line">        &#125;</div><div class="line">    ]</div><div class="line">&#125;</div><div class="line">&gt; rs.initiate(config)</div><div class="line">&#123; <span class="string">"ok"</span> : 1 &#125;</div><div class="line">shard2:OTHER&gt; rs.status</div><div class="line"><span class="function"><span class="title">function</span></span> () &#123; <span class="built_in">return</span> db._adminCommand(<span class="string">"replSetGetStatus"</span>); &#125;</div><div class="line">shard2:PRIMARY&gt; rs.status()</div><div class="line">&#123;</div><div class="line">    <span class="string">"set"</span> : <span class="string">"shard2"</span>,</div><div class="line">    <span class="string">"date"</span> : ISODate(<span class="string">"2016-09-08T04:48:48.591Z"</span>),</div><div class="line">    <span class="string">"myState"</span> : 1,</div><div class="line">    <span class="string">"members"</span> : [</div><div class="line">        &#123;</div><div class="line">            <span class="string">"_id"</span> : 0,</div><div class="line">            <span class="string">"name"</span> : <span class="string">"192.168.100.178:27018"</span>,</div><div class="line">            <span class="string">"health"</span> : 1,</div><div class="line">            <span class="string">"state"</span> : 1,</div><div class="line">            <span class="string">"stateStr"</span> : <span class="string">"PRIMARY"</span>,</div><div class="line">            <span class="string">"uptime"</span> : 3472,</div><div class="line">            <span class="string">"optime"</span> : Timestamp(1473310118, 1),</div><div class="line">            <span class="string">"optimeDate"</span> : ISODate(<span class="string">"2016-09-08T04:48:38Z"</span>),</div><div class="line">            <span class="string">"electionTime"</span> : Timestamp(1473310122, 1),</div><div class="line">            <span class="string">"electionDate"</span> : ISODate(<span class="string">"2016-09-08T04:48:42Z"</span>),</div><div class="line">            <span class="string">"configVersion"</span> : 1,</div><div class="line">            <span class="string">"self"</span> : <span class="literal">true</span></div><div class="line">        &#125;,</div><div class="line">        &#123;</div><div class="line">            <span class="string">"_id"</span> : 1,</div><div class="line">            <span class="string">"name"</span> : <span class="string">"192.168.100.180:27018"</span>,</div><div class="line">            <span class="string">"health"</span> : 1,</div><div class="line">            <span class="string">"state"</span> : 2,</div><div class="line">            <span class="string">"stateStr"</span> : <span class="string">"SECONDARY"</span>,</div><div class="line">            <span class="string">"uptime"</span> : 9,</div><div class="line">            <span class="string">"optime"</span> : Timestamp(1473310118, 1),</div><div class="line">            <span class="string">"optimeDate"</span> : ISODate(<span class="string">"2016-09-08T04:48:38Z"</span>),</div><div class="line">            <span class="string">"lastHeartbeat"</span> : ISODate(<span class="string">"2016-09-08T04:48:46.711Z"</span>),</div><div class="line">            <span class="string">"lastHeartbeatRecv"</span> : ISODate(<span class="string">"2016-09-08T04:48:46.722Z"</span>),</div><div class="line">            <span class="string">"pingMs"</span> : 0,</div><div class="line">            <span class="string">"syncingTo"</span> : <span class="string">"192.168.100.178:27018"</span>,</div><div class="line">            <span class="string">"configVersion"</span> : 1</div><div class="line">        &#125;,</div><div class="line">        &#123;</div><div class="line">            <span class="string">"_id"</span> : 2,</div><div class="line">            <span class="string">"name"</span> : <span class="string">"192.168.100.142:27018"</span>,</div><div class="line">            <span class="string">"health"</span> : 1,</div><div class="line">            <span class="string">"state"</span> : 7,</div><div class="line">            <span class="string">"stateStr"</span> : <span class="string">"ARBITER"</span>,</div><div class="line">            <span class="string">"uptime"</span> : 9,</div><div class="line">            <span class="string">"lastHeartbeat"</span> : ISODate(<span class="string">"2016-09-08T04:48:46.711Z"</span>),</div><div class="line">            <span class="string">"lastHeartbeatRecv"</span> : ISODate(<span class="string">"2016-09-08T04:48:46.721Z"</span>),</div><div class="line">            <span class="string">"pingMs"</span> : 0,</div><div class="line">            <span class="string">"configVersion"</span> : 1</div><div class="line">        &#125;</div><div class="line">    ],</div><div class="line">    <span class="string">"ok"</span> : 1</div><div class="line">&#125;</div><div class="line">shard2:PRIMARY&gt;</div></pre></td></tr></table></figure></p>
<h2 id="配置Config-Server"><a href="#配置Config-Server" class="headerlink" title="配置Config Server"></a>配置Config Server</h2><p>提供配置文件,以服务器A为例，B和C参考A</p>
<p><strong>config_server.conf</strong><br><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line">configsvr=<span class="literal">true</span></div><div class="line">dbpath=/data/mongodb/config</div><div class="line">logpath=/data/mongodb/logs/config_server.log</div><div class="line">pidfilepath=/data/mongodb/config/config_server.pid</div><div class="line">logappend=<span class="literal">true</span></div><div class="line">bind_ip=192.168.100.142</div><div class="line">port=20000</div><div class="line">fork=<span class="literal">true</span></div></pre></td></tr></table></figure></p>
<p>在服务器A、B和C上都执行：<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">[root@localhost config]<span class="comment"># mongod -f config_server.conf</span></div><div class="line">about to fork child process, waiting until server is ready <span class="keyword">for</span> connections.</div><div class="line">forked process: 2251</div><div class="line">child process started successfully, parent exiting</div><div class="line">[root@localhost config]<span class="comment">#</span></div></pre></td></tr></table></figure></p>
<h2 id="配置Route-Process"><a href="#配置Route-Process" class="headerlink" title="配置Route Process"></a>配置Route Process</h2><p>在服务器A创建mongos目录<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">mkdir /data/mongodb/mongos</div><div class="line"><span class="built_in">cd</span> /data/mongodb/mongos</div></pre></td></tr></table></figure></p>
<p>提供配置文件<br><strong>mongos.conf</strong><br><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line">port=30000</div><div class="line">bind_ip=192.168.100.142</div><div class="line">logpath=/data/mongodb/logs/mongos.log</div><div class="line">pidfilepath=/data/mongodb/mongos/mongos.pid</div><div class="line">logappend=<span class="literal">true</span></div><div class="line">fork=<span class="literal">true</span></div><div class="line">chunkSize=5</div></pre></td></tr></table></figure></p>
<h1 id="启动mongos"><a href="#启动mongos" class="headerlink" title="启动mongos"></a>启动mongos</h1><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">mongos --configdb 192.168.100.142:20000,192.168.100.178:20000,192.168.100.180:20000 -f mongos.conf</div></pre></td></tr></table></figure>
<h1 id="配置Shard-Cluster"><a href="#配置Shard-Cluster" class="headerlink" title="配置Shard Cluster"></a>配置Shard Cluster</h1><p>连接mongos的30000端口，进行以下配置<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div></pre></td><td class="code"><pre><div class="line">[root@localhost mongos]<span class="comment"># mongo 192.168.100.142:30000</span></div><div class="line">MongoDB shell version: 3.0.6</div><div class="line">connecting to: 192.168.100.142:30000/<span class="built_in">test</span></div><div class="line">Server has startup warnings:</div><div class="line">2016-09-08T14:26:28.843+0800 I CONTROL  ** WARNING: You are running this process as the root user, <span class="built_in">which</span> is not recommended.</div><div class="line">2016-09-08T14:26:28.843+0800 I CONTROL </div><div class="line">mongos&gt; use admin</div><div class="line">switched to db admin</div><div class="line">mongos&gt; db.runCommand(&#123;addshard:<span class="string">"shard1/192.168.100.142:27017,192.168.100.178:27017,192.168.100.180:27017"</span>&#125;);</div><div class="line">&#123; <span class="string">"shardAdded"</span> : <span class="string">"shard1"</span>, <span class="string">"ok"</span> : 1 &#125;</div><div class="line">mongos&gt;  db.runCommand(&#123;addshard:<span class="string">"shard2/192.168.100.142:27018,192.168.100.178:27018,192.168.100.180:27018"</span>&#125;);</div><div class="line">&#123; <span class="string">"shardAdded"</span> : <span class="string">"shard2"</span>, <span class="string">"ok"</span> : 1 &#125;</div></pre></td></tr></table></figure></p>

        </div>
        <div id="post-footer">
            <div class="avatar" >
                <img src="/img/author.jpg" alt="avatar"/>
                <!-- 
                <a href="javascript:void(0)" class="high-song">high起来 &#128541;</a>
                 -->
                
                <a href="http://backup-miabon.oss-cn-hongkong.aliyuncs.com/img/alipay.jpg" target="_blank" class="donate fa">打赏小弟 &#128536;</a>
                
            </div>
            <ul class="author-profile-section">
                <li>
                  
                  作者:
                  
                    
                    <a href="/about.html">MIAbon</a>
                </li>
                
                <li>发表日期: <span>2016-10-31  00:00:00</span></li>
                
                <li>最后编辑日期: <span>2017-11-23  08:23:00</span></li>
                
                <li class="post-category">
                    文章分类:
                    
                    <a href="/categories/mongodb/">mongodb</a>
                    
                </li>
                <li class="post-tags">
                    文章标签:
                    
                    <a href="/tags/linux/">linux</a>
                    
                </li>
                
                <li> 版权声明: <a href="https://creativecommons.org/licenses/by-nc-nd/3.0/" target="_blank">
知识共享署名-非商业性使用-禁止演绎 3.0 未本地化版本许可协议（CC BY-NC-ND 3.0）
</a></li>
                
            </ul>
            <div id="donate-wrap">
                
                
                
                <img src="http://www.geasslinks.com/img/alipay.jpg" alt="支付宝付款" class="donate-img">
                
                
            </div>
        </div>
    </article>
    <div class="article-nav">
        
        <a href="/2016/11/29/CentOS7升级内核/" class="pre-post fa fa-caret-left">CentOS7升级内核</a>
        
        
        <a href="/2016/10/26/在EC2_Linux上挂载S3存储桶/" class="next-post fa">在EC2 Linux上挂载S3存储桶</a>
        
    </div>
    
    <div id="comments">
        

<script>
  gitment.render(document.getElementById("comments"));
</script>



    </div>
    
</div>


    </div>
    <footer id="footer">
    
    <div class="social">
        
        <a href="https://www.example1.com" class="fa fa-free-code-camp" target="_blank" title="freecodecamp"></a>
        
        <a href="https://github.com/werewolf2101" class="fa fa-github" target="_blank" title="Follow me~"></a>
        
        <a href="mailto:werewolf2101@gmail.com" class="fa fa-email" target="_blank" title="Email"></a>
        
    </div>
    
    <div>
        
        <a href="/" class="copyright-links">MIAbon</a>&copy;2015 - 2018.All Rights
        Reserved.
    </div>
    <p>Powered by <a href="https://hexo.io" class="copyright-links" target="_blank">Hexo</a> | Theme by <a
                href="https://github.com/GeekaholicLin" class="copyright-links" target="_blank">GeekaholicLin</a>
    </p>
    
    
    <p>
        <span id="busuanzi_container_site_uv" class="fa fa-bar-chart">
        欢迎第<span id="busuanzi_value_site_uv"><i class="fa fa-spinner fa-spin"></i></span>位小伙伴~
        </span>
    </p>
    
</footer>

</div>
    <ul id="tools">
    <li class="totop-btn fa fa-angle-up"></li>
    <li class="exchange-btn fa fa-exchange"></li>
  
    <li class="toc-btn fa fa-list-ul"></li>
    
    

    
</ul>
<p id="process"></p>
<div id="search-overlay">
    <div class="search-area-wrap">
        <div id="search-area">
            <div class="input-wrap focus">
                <i class="fa fa-search" aria-hidden="true"></i>
                <input id="search-input" autofocus autocomplete="off" type="text"
                       placeholder="search this website..."/>
            </div>
            <ul id="search-result">
                <li class="load-first"><i class="fa fa-spinner fa-pulse"></i></li>
            </ul>
        </div>
    </div>
</div>

    <ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#mongodb安装"><span class="toc-number">1.</span> <span class="toc-text">mongodb安装</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#NTP时间同步（所有服务器时间必须同步否则集群无法建立）"><span class="toc-number">1.1.</span> <span class="toc-text">NTP时间同步（所有服务器时间必须同步否则集群无法建立）</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#创建数据目录"><span class="toc-number">1.2.</span> <span class="toc-text">创建数据目录</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#在服务器A上"><span class="toc-number">1.2.1.</span> <span class="toc-text">在服务器A上</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#在服务器B上"><span class="toc-number">1.2.2.</span> <span class="toc-text">在服务器B上</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#在服务器C上"><span class="toc-number">1.2.3.</span> <span class="toc-text">在服务器C上</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#启动分片1"><span class="toc-number">1.3.</span> <span class="toc-text">启动分片1</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#启动分片2"><span class="toc-number">1.4.</span> <span class="toc-text">启动分片2</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#配置Config-Server"><span class="toc-number">1.5.</span> <span class="toc-text">配置Config Server</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#配置Route-Process"><span class="toc-number">1.6.</span> <span class="toc-text">配置Route Process</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#启动mongos"><span class="toc-number">2.</span> <span class="toc-text">启动mongos</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#配置Shard-Cluster"><span class="toc-number">3.</span> <span class="toc-text">配置Shard Cluster</span></a></li></ol>


    <script src="/js/highsong.js"></script>



<script src="/js/search.js"></script>
<script type="text/javascript">
    //theme config datas
    var copyrightObj = {};
    copyrightObj.enable = 'true';
    copyrightObj.triggerCopyLength = '200';
    copyrightObj.appendText = '商业转载请联系作者获得授权,非商业转载请注明出处 © gasslinks.com';
    var leancloudObj = {};
    leancloudObj.enable = 'true';
    leancloudObj.className = 'blog';
    leancloudObj.limits = '5';
</script>
<script>
var _hmt = _hmt || [];
(function() {
  var hm = document.createElement("script");
  hm.src = "https://hm.baidu.com/hm.js?dfebe5842f5d4196471b84802392a4b2";
  var s = document.getElementsByTagName("script")[0]; 
  s.parentNode.insertBefore(hm, s);
})();
</script>
<script type="text/javascript">
    var search = {};
    var search_path = "search.xml";
    if (!search_path) {
        search_path = "search.xml";
    }
    search.path = "/" + search_path;
    search.func =  _ajax.init();
</script>
<script src="/js/app.js"></script>


</body>
</html>